﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using ERP_RORZE_TOOL.Tool.UserControls;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE_TOOL.Tool.Forms
{
    public partial class frm_JustNewAdd : DevExpress.XtraEditors.XtraForm
    {
        private DataTable dt = new DataTable();
        private ucJustNew parent;
        private ucImport parentImport;
        private bool firstLoadFromImport = true;
        public string ToolId;
        public bool FromImport;
        public int import;

        public frm_JustNewAdd(ucJustNew fJustNew=null, ucImport fImport = null, bool isFromImport = false)
        {
            InitializeComponent();
            btTAdd.Image = ERP_RORZE_TOOL.Properties.Resources.add;
            btTCancel.Image = ERP_RORZE_TOOL.Properties.Resources.cancel;
            parent = fJustNew;
            parentImport = fImport;
            FromImport = isFromImport;
            this.CancelButton = this.btTCancel;
        }

        private void XtraForm1_Load(object sender, EventArgs e)
        {
            if (FromImport)
            {
                dtpTDate.Value = DateTime.Today;
                BindDataFromImport();
                firstLoadFromImport = false;
            }
            else
            {
                dtpTDate.Value = DateTime.Today;
                BindDataTool();
            }

        }

        private void btTAdd_Click_1(object sender, EventArgs e)
        {
            AddTool();
        }

        private void btTCancel_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmbToolID_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbToolID.SelectedItem != cmbToolID.Items[0] && !FromImport)
            {
                import = CheckInput();
                MessageBox.Show(string.Format(ToolConstants.Message.JustNew.ImportQuantity, cmbToolID.SelectedValue, import.ToString()), ToolConstants.Form.MessageBox.Message, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (cmbToolID.SelectedItem != cmbToolID.Items[0] && FromImport && !firstLoadFromImport)
            {
                import = CheckInput();
                MessageBox.Show(string.Format(ToolConstants.Message.JustNew.ImportQuantity, cmbToolID.SelectedValue, import.ToString()), ToolConstants.Form.MessageBox.Message, MessageBoxButtons.OK, MessageBoxIcon.Information);
                
            }
            
        }

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }
        #region Private methods

        private bool GetDataTool(JustNew o)
        {
            #region Check valid

            if (Utilities.IsNullOrEmpty(dtpTDate.Value.ToString()))
            {
                MessageBox.Show(ToolConstants.Message.JustNew.DateCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbToolID.SelectedValue) || cmbToolID.SelectedItem == cmbToolID.Items[0])
            {
                MessageBox.Show(ToolConstants.Message.JustNew.ToolIDCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtTQuantity.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.JustNew.QuantityCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (int.Parse(txtTQuantity.Text) <= 0)
            {
                MessageBox.Show(ToolConstants.Message.JustNew.QuantityMustGreaterZero, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTQuantity.Focus();
                return false;

            }

            if (int.Parse(txtTQuantity.Text) > 0)
            {
                string fieldName = ToolConstants.StoreProcedure.FieldName.DaoMoi;
                string where = ToolConstants.StoreProcedure.FieldName.ToolID + "='" + cmbToolID.SelectedValue + "'";
                string storedName = ToolConstants.StoreProcedure.FTonKhoTongHop;

                int import = Utilities.CheckInput(fieldName, storedName, where);

                if (import < int.Parse(txtTQuantity.Text))
                {
                    MessageBox.Show(string.Format(ToolConstants.Message.JustNew.WrongQuantity, cmbToolID.SelectedValue, import.ToString(), txtTQuantity.Text), ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtTQuantity.Text = "";
                    txtTQuantity.Focus();
                    return false;

                }

            }
            #endregion

            o.Date = dtpTDate.Value;
            o.ToolID = cmbToolID.SelectedValue.ToString();
            o.Quantity = int.Parse(txtTQuantity.Text);
            o.Note = memoTNote.Text;

            return true;
        }

        private void BindDataTool()
        {
            ToolHelper helper = new ToolHelper(new Tools());
            dt = helper.GetAllByToolType();
            DataRow row = dt.NewRow();

            

            row[ToolConstants.Entities.Tool.FieldName.FullName] = "Select...";
            row[ToolConstants.Entities.Tool.FieldName.ToolID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbToolID.DataSource = dt;
            cmbToolID.DisplayMember = ToolConstants.Entities.Tool.FieldName.FullName;
            cmbToolID.ValueMember = ToolConstants.Entities.Tool.FieldName.ToolID;

            if (!FromImport)
            {
                cmbToolID.SelectedItem = cmbToolID.Items[0];
            }
        }

        private void AddTool()
        {
            JustNewHelper helper = new JustNewHelper(new JustNew());
            if (GetDataTool(helper.ValueObject))
            {



                if (helper.Insert() > 0)
                {

                    if (Utilities.MessageConfirmationYesNo(ToolConstants.Message.AddSuccessAndQuestion))
                    {

                        dtpTDate.Value = DateTime.Now;
                        cmbToolID.SelectedItem = cmbToolID.Items[0];
                        txtTQuantity.Text = "";
                        memoTNote.Text = "";
                        //lblStatus.Visible = false;
                        dtpTDate.Focus();
                    }
                    else
                    {
                        this.Close();
                    }
                }
                else
                {

                    Utilities.MessageErrorOk(ToolConstants.Message.AddFailed);
                }
                if (parent!=null)
                {
                    parent._action = 0;
                    parent.BindDatatoGrid();
                }
                if (FromImport)
                {
                    parentImport.BindDatatoGridJustNew();
                }

            }
        }

        private void BindDataFromImport()
        {
            BindDataTool();

            cmbToolID.SelectedValue = ToolId;
            if (cmbToolID.SelectedItem == null)
            {
                cmbToolID.SelectedItem = cmbToolID.Items[0];
            }
            //import = CheckInput();
            //lblStatus.Visible = true;
            //lblStatus.Text = string.Format(ToolConstants.Message.JustNew.ImportQuantity, cmbToolID.SelectedValue,
            //                               import.ToString());

        }

        private int CheckInput()
        {
            string fieldName = ToolConstants.StoreProcedure.FieldName.DaoMoi;
            string where = ToolConstants.StoreProcedure.FieldName.ToolID + "='" + cmbToolID.SelectedValue + "'";
            string storedName = ToolConstants.StoreProcedure.FTonKhoTongHop;

            return Utilities.CheckInput(fieldName, storedName, where);

        }

        #endregion

        
    }
}